package rocket.demo.sync;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

public class ProducerDemo {
  public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, MQBrokerException, RemotingException, InterruptedException {
    // 创建一个生产者
    DefaultMQProducer producer = new DefaultMQProducer("group_test");
    
    // 设置NameServer 地址
    producer.setNamesrvAddr("192.168.56.12:9876");
    producer.start();
    
    
    for (int i = 0; i < 10; i++) {
      Message message = new Message("test001", "tag", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
      SendResult sendResult = producer.send(message);
      System.out.printf("%s%n", sendResult);
    }
    
    producer.shutdown();
  }
}
